package com.design.adnroid.campussecondarytrading.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.design.adnroid.campussecondarytrading.AppContext;
import com.design.adnroid.campussecondarytrading.database.DBOperate;
import com.design.adnroid.campussecondarytrading.entity.Goods;

public class GoodsActivity extends AppCompatActivity {

    private LinearLayout layout_all;
    private TextView tvGoodsName, tvGoodsText, tvGoodsPrice, tvGoodsTime;
    private TextView tvGoodsOwner, tvGoodsOwnerContact;
    private TextView tvGoodsBuyer, tvGoodsState;
    private ImageView ivGoodsImage;
    private Button btButton, btFinish;

    private Goods goods;
    private DBOperate dbOperate;

    private Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        initID();

        intent = getIntent();
        String goodsId = intent.getStringExtra("intentGoodsId");

        dbOperate = new DBOperate(this);
        dbOperate.openDB();
        goods = dbOperate.getGoods(goodsId);
        dbOperate.closeDb();

        initView();

    }//onCreate

    private void initView() {
        final String[] str = new String[]{"发布", "买家下单", "卖家确认,交易中", "买家确认收货,交易完成", "已下架", "交易取消"};

        tvGoodsName.setText(goods.getGoodsName());
        tvGoodsText.setText(goods.getGoodsText());
        tvGoodsPrice.setText(String.valueOf(goods.getGoodsPrice()));
        tvGoodsOwner.setText(goods.getGoodsOwner());
        tvGoodsTime.setText(goods.getGoodsTime());
        tvGoodsOwnerContact.setText(goods.getGoodsOwnerContact());

        tvGoodsBuyer.setText(goods.getGoodsBuyers());
        tvGoodsState.setText(str[goods.getGoodsState()]);

        ivGoodsImage.setImageBitmap(goods.getGoodsImage());

        layout_all.setVisibility(View.GONE);
        btFinish.setVisibility(View.GONE);
//        /*
//         *    goodsState
//         *       0               发布
//         *       0 -> 1          买家下单
//         *       1 -> 2          卖家确认,交易中
//         *       2 -> 3          买家确认收货,交易完成
//         *       4               已下架
//         *       5               取消交易
//         */
        if (goods.getGoodsOwner().equals(AppContext.getUserName())) {
            switch (goods.getGoodsState()) {
                case 0:
                    btButton.setText("下架");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState("", 4);
                            Toast.makeText(getApplicationContext(), "下架成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 1:
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("确认卖出");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState(goods.getGoodsBuyers(), 2);
                            Toast.makeText(getApplicationContext(), "确认卖出成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 2:
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("取消交易");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState(goods.getGoodsBuyers(), 5);
                            Toast.makeText(getApplicationContext(), "取消交易成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 3://交易完成
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("删除记录");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dbOperate.openDB();
                            dbOperate.deleteGoods(goods);
                            dbOperate.closeDb();
                            Toast.makeText(getApplicationContext(), "删除记录成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 4://已下架
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("重新发布");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState("", 0);
                            backToList();
                            Toast.makeText(getApplicationContext(), "重新发布成功！", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                case 5://取消交易
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("重新发布");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState("", 0);
                            backToList();
                            Toast.makeText(getApplicationContext(), "重新发布成功！", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                default:
            }//switch
        } else if (goods.getGoodsBuyers().equals(AppContext.getUserName())) {
            switch (goods.getGoodsState()) {
                case 1:
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("取消下单");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState("", 0);
                            Toast.makeText(getApplicationContext(), "取消下单成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 2:
                    layout_all.setVisibility(View.VISIBLE);
                    btFinish.setVisibility(View.VISIBLE);
                    btButton.setText("取消交易");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState(goods.getGoodsBuyers(), 5);
                            Toast.makeText(getApplicationContext(), "取消交易成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    btFinish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState(goods.getGoodsBuyers(), 3);
                            Toast.makeText(getApplicationContext(), "确认收货成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 3://交易完成
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("删除记录");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dbOperate.openDB();
                            dbOperate.deleteGoods(goods);
                            dbOperate.closeDb();
                            Toast.makeText(getApplicationContext(), "删除记录成功！", Toast.LENGTH_SHORT).show();
                            backToList();
                        }
                    });
                    break;
                case 5://取消交易
                    layout_all.setVisibility(View.VISIBLE);
                    btButton.setText("重新购买");
                    btButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            upGoodsBuyersState(AppContext.getUserName(), 1);
                            backToList();
                            Toast.makeText(getApplicationContext(), "重新购买成功！", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                default:
            }//switch
        } else {
            btButton.setText("下单");
            btButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String buyers = AppContext.getUserName();
                    upGoodsBuyersState(buyers, 1);
                    Toast.makeText(getApplicationContext(), "下单成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(GoodsActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
        }//if-else

    }

    private void backToList() {
        if (TextUtils.isEmpty(intent.getStringExtra("intentName"))) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }
        Intent newintent = new Intent(this, GoodsListActivity.class);
        String intentName= intent.getStringExtra("intentName");
        String intentPosition = intent.getStringExtra("intentPosition");

        newintent.putExtra("intentName", intentName);
        newintent.putExtra("intentPosition", intentPosition);
        startActivity(newintent);
        finish();
    }

    private void upGoodsBuyersState(String buyers, int state) {
        dbOperate.openDB();
        goods.setGoodsState(state);
        goods.setGoodsBuyers(buyers);
        dbOperate.upGoods(goods);
        dbOperate.closeDb();
    }

    private void initID() {

        layout_all = findViewById(R.id.goods_layoutAll);

        tvGoodsName = findViewById(R.id.goods_goodsName);
        tvGoodsText = findViewById(R.id.goods_goodsText);
        tvGoodsPrice = findViewById(R.id.goods_goodsPrice);
        tvGoodsTime = findViewById(R.id.goods_goodsTime);
        tvGoodsOwner = findViewById(R.id.goods_goodsOwner);
        tvGoodsOwnerContact = findViewById(R.id.goods_goodsOwnerContact);

        tvGoodsBuyer = findViewById(R.id.goods_goodsBuyer);
        tvGoodsState = findViewById(R.id.goods_goodsState);

        btButton = findViewById(R.id.goods_goodsButton);
        btFinish = findViewById(R.id.goods_goodsFinish);
        ivGoodsImage = findViewById(R.id.goods_goodsImage);
    }
}
